#include "SoldierSniper.h"


SoldierSniper::SoldierSniper(float X, float Y)
{
	m_Sprite = Source::getInstance()->getSprite(IDImage::IMG_SoldierSniper_90);
	m_Position.x = (float)X;
	m_Position.y = (float)Y;

	m_TimeAnimation = 1.0f;
	m_Gametime = new GameTime();

	loadPositionGunAtSprite("Enemy//PositionGunSoldier.txt");
	m_ToolBullet = new Toolbullet(3, 3.0f);
	m_ToolBullet->setGun(IDGun::Enemy_Gun);
}


void SoldierSniper::UpdateAnimation(float X, float Y)
{
	m_Gametime->setTime();
	if (m_Gametime->getElapsedTimePerSecond() >= m_TimeAnimation)
	{
		if (abs(m_Vpx - m_Position.x) >= SCREEN_WIDTH) return;

		float x = X - m_Position.x;
		float y = Y - m_Position.y;

		if (x == 0) x++;
		if (x >= 0)
			m_Flip = SpriteEffect::None;
		else
			m_Flip = SpriteEffect::Flip;

		if (Y >= m_Position.y)
		{
			TanX = abs(y / x);
			m_IdImage = IDImage::IMG_SoldierSniper_45;
			if (TanX > 0 && TanX <= tan(PI/10))
			{
				m_IdImage = IDImage::IMG_SoldierSniper_90;
				m_shootDirection = ShootDirection::_0_SHOOT;
			}
			else if (TanX > tan(PI / 10) && TanX <= tan(PI / 5))
			{			
				m_shootDirection = ShootDirection::_30_SHOOT;
			}
			else if (TanX > tan(PI / 5) && TanX <= tan(3*PI / 10))
			{
				m_shootDirection = ShootDirection::_45_SHOOT;
			}
			else if (TanX > tan(3 * PI / 10) && TanX <= tan(2 * PI / 5))
			{
				m_shootDirection = ShootDirection::_60_SHOOT;
			}
			else if (TanX > tan(2 * PI / 5))
			{
				m_shootDirection = ShootDirection::_90_SHOOT;
			}
		}
		else if (Y < m_Position.y)
		{
			TanX = -abs(y / x);
			m_IdImage = IDImage::IMG_SoldierSniper_135;
			if (TanX <= 0 && TanX >= tan(-PI / 10))
			{
				m_IdImage = IDImage::IMG_SoldierSniper_90;
				m_shootDirection = ShootDirection::_0_SHOOT;
			}
			else if (TanX < tan(-PI / 10) && TanX >= tan(-PI / 5))
			{
				m_shootDirection = ShootDirection::_330_SHOOT;
			}
			else if (TanX < tan(-PI / 5) && TanX >= tan(-3 * PI / 10))
			{
				m_shootDirection = ShootDirection::_315_SHOOT;
			}
			else if (TanX < tan(-3 * PI / 10) && TanX >= tan(-2 * PI / 5))
			{
				m_shootDirection = ShootDirection::_300_SHOOT;
			}
			else if (TanX < tan(-2 * PI / 5))
			{
				m_shootDirection = ShootDirection::_270_SHOOT;
			}
		}
		m_Sprite = Source::getInstance()->getSprite(m_IdImage);
	}
}

void SoldierSniper::Shoot(int i)
{
	m_ToolBullet->InitBullet(m_Position.x, m_Position.y, m_Sprite->getWidth(), m_Sprite->getHeight(), m_positionShootAtSprite[i].m_X, m_positionShootAtSprite[i].m_Y, m_Flip, m_shootDirection, TypeMove::Move_OnTheLand);
	m_ToolBullet->Shoot();
}

void SoldierSniper::UpdateVeclocity()
{
	m_ToolBullet->UpdateVeclocity();
}

void SoldierSniper::Update()
{
	if (m_Gametime->getElapsedTimePerSecond() >= m_TimeAnimation)
	{
		m_Gametime->Update();
		
		for (int i = 0; i < m_positionShootAtSprite.size(); i++)
		{
			if (m_IdImage == IDImage::IMG_SoldierSniper_45 && m_positionShootAtSprite[i].m_Index == ID_POSITION_GUN_SOLDIERSNIPER_45)
			{
				Shoot(i);
			}
			else if (m_IdImage == IDImage::IMG_SoldierSniper_90 && m_positionShootAtSprite[i].m_Index == ID_POSITION_GUN_SOLDIERSNIPER_0)
			{
				Shoot(i);
			}
			else if (m_IdImage == IDImage::IMG_SoldierSniper_135 && m_positionShootAtSprite[i].m_Index == ID_POSITION_GUN_SOLDIERSNIPER_315)
			{
				Shoot(i);
			}
		}
	}

	m_ToolBullet->Update();
}

void SoldierSniper::Draw(LPD3DXSPRITE SpriteHandler)
{
	Character::Draw(SpriteHandler);
	if (m_TimeShoot > 0)
	{
		m_TimeShoot -= m_TimeAnimation;
		m_Sprite->Next();
	}
}



SoldierSniper::~SoldierSniper()
{
}
